package com.xcl.service.impl;

import com.xcl.mapper.CentreMapper;
import com.xcl.pojo.Centre;
import com.xcl.service.CentreServiceInter;
import com.zzg.entity.*;
import com.zzg.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

/**
 * @ClassName CentreServiceImpl
 * @Description 分流中心服务层实现类
 * @Author: xiaochaolei
 * @date 2021/7/2 15:28
 * @Version 1. 0
 */
@Service
public class CentreServiceImpl implements CentreServiceInter {

    @Autowired
    CentreMapper centreMapper;

    @Override
    public List<Centre> getAllCentre() {
        return centreMapper.getAllCentre();
    }

    @Override
    public List<Centre> getAllCentreByLimit(int page, int limit) {
        return centreMapper.getAllCentreByLimit((page-1)*limit,limit);
    }

    @Override
    public List<Centre> getCentreByWhere(String waybill_id, String sub_name, String modeTransport, String TypeTransportation, String remark, int page, int limit) {
        return centreMapper.getCentreByWhere(waybill_id,sub_name,modeTransport,TypeTransportation,remark,(page-1)*limit,limit);
    }

    @Override
    public int upteModification(String[] strings) {
        return centreMapper.upteModification(strings);
    }

    @Override
    public Centre getFreightinformationTableByid(String id) {
        return centreMapper.getFreightinformationTableByid(id);
    }

    @Override
    public int insertDelivery(Delivery delivery, Driver driver,String waybill_id) {
        //用于存储车辆id
        String id="";
        //得到车辆牌照
        String licensetag = driver.getLicensetag();
        //判断是否存在
        Driver driver1 = centreMapper.existsDriver(licensetag);
        //该车辆不存在
        if (driver1==null){
            //得到uuid
            id= UuidUtil.getUuid();
            //设置车辆id
            driver.setDriverId(id);
            //新增车辆
            centreMapper.insertDriver(driver);
        }else {
            //存在车辆就得到id
            id=centreMapper.existsDriver(licensetag).getDriverId();
        }
        //将车辆id设置到里面订单派送去
        delivery.setDeliveryvehicles(id);
        //设置运单派送id
        delivery.setDeliveryId(UuidUtil.getUuid());
        //新增订单派送
        int i=centreMapper.insertDelivery(delivery);
        //得到运单id
        String[] split = waybill_id.split("-");
        //修改状态为派送中
        centreMapper.upteModification(split);

        //新增运单跟踪
        //new 跟踪对象
        Tailafter tailafter=new Tailafter();
        //得到运单id
        tailafter.setWaybillId(waybill_id);
        //设置id
        tailafter.setTailafter(UuidUtil.getUuid());
        //操作
        tailafter.setState("运单派送");
        //得到当前时间
        String TimeNow = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
        //设置时间
        tailafter.setTailafterTime(TimeNow);
        //调用运单跟踪
        centreMapper.insertTailafter(tailafter);
        //得到id
        String cid=UuidUtil.getUuid();
        //实例化费用明细模型类
        Costbreakdown costbreakdown=new Costbreakdown();
        //设置id
        costbreakdown.setCostbreakdownId(cid);
        //设置未结清金额
        costbreakdown.setOwefee(delivery.getCost());
        //设置应该收取的费用
        costbreakdown.setReceivables(delivery.getCost());
        //设置未收
        costbreakdown.setRealizefee(0.0);
        //新增费用明细
        centreMapper.insertCostbreakdown(costbreakdown);
        //实例化财务管理
        Finance finance=new Finance();
        //得到id并且设置id
        finance.setFinance(UuidUtil.getUuid());
        //得到费用详细id设置id
        finance.setCostbreakdownId(cid);
        //设置订单状态未派送订单
        finance.setOrdertype("派送订单");
        //将运单id设置到里面去
        finance.setOrdernumber(delivery.getDeliveryId());
        //财务状态未付款
        finance.setOrderstatus("未付款");
        //新增
        centreMapper.insertFinance(finance);
        //返回
        return i;
    }

}
